<template>
	<view>
		<button open-type="getPhoneNumber" @getphonenumber="handleGetPhone">快速登录</button>
		<up-button @click="goPhoneLogin">手机号验证码登录</up-button>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
import { GET_ACCESS_TOKEN, GET_PHONE_NUMBER } from '../../api/login';
import { DO_LOGIN } from '../../api/codeLogin';

	const handleGetPhone = (phoneInfo) => {
		console.log(phoneInfo)
		const code = phoneInfo.detail.code // 你的手机号加密后的字符串
		
		uni.request({
			url: 'https://api.weixin.qq.com/cgi-bin/token',
			method: 'GET',
			data: {
				grant_type: 'client_credential',
				appid: 'wx52d48be4d68f58a2',
				secret: 'e1e5894c7b5a4e005f84737be6b8ee31'
			},
			success(res) {
				console.log(res)
				const accessToken = res.data.access_token
				uni.request({
					url: 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' + accessToken,
					method: 'POST',
					data: {
						code
					},
					success(phoneRes) {
						console.log(phoneRes)
						const phoneNum = phoneRes.data.phone_info.phoneNumber
						DO_LOGIN(phoneNum).then(res => {
							console.log(res)
							uni.setStorageSync('phone', res.phone)
							uni.setStorageSync('token', res.token.token)
							uni.navigateTo({
								url: '/pages/my/my'
							})
						})
					}
				})
			}
		})
		
		
		
	}
	const goPhoneLogin = () => {
		uni.navigateTo({
			url: '/pagesA/codeLogin/codeLogin'
		})
	}
</script>

<style lang="scss" scoped>
	.login-wrap {
		width: 80%;
		margin: 0 auto;
	}
</style>